<?php

class cart extends cms_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('cart_model', 'basket');
        $this->load->model('invoicie_items_model', 'invitem');
    }

    public function index() {
        $data = $this->basket->get(array('session_id' => $this->session->userdata('session_id')), false, 'row');
        if (count($data) > 0) {
            $items = $this->invitem->get(array('inv_num' => $data->inv_num));
            if ($items) {
                if ($this->user_id) {
                    $this->load->model('address_model');
                    $this->pass['address'] = $address = $this->address_model->get(array('user_id' => $this->user_id));
                    $cities = get_shiping_cities_id();
                    foreach ($items as $item) {
                        if (!$item->city_id) {
                            $product = get_product_from_id($item->product_id);
                            foreach ($address as $adr) {
                                if ($adr->default == 'YES') {
                                    if (in_array($adr->cities_id, $cities)) {
                                        $weghite = $product->shiping_weghite * $item->amount;
                                        $max = get_shiping_max_weight();
                                        $price = get_shiping_price();
                                        if ($weghite <= $max) {
                                            $shiping = $price;
                                        } else {
                                            $weghitover = $weghite - $max;
                                            $shiping = $price + ($weghitover * get_shiping_price_for_unit());
                                        }
                                        if ($product->shiping_on == 'DEALER')
                                            $shiping = 0;
                                        elseif ($product->shiping_on == 'BETWEEN')
                                            $shiping = $shiping / 2;

                                        $save['address'] = $adr->id;
                                        $save['city_id'] = $adr->cities_id;
                                        $save['mobile'] = $adr->mobile;
                                        $save['shiping'] = $shiping;
                                        $save['weight'] = $weghite;
                                        $save['total_price'] = $item->price + $shiping;
                                        $this->invitem->save($save, array('id' => $item->id), TRUE);
                                    } else {
                                        $save['address'] = $adr->id;
                                        $save['city_id'] = $adr->cities_id;
                                        $save['mobile'] = $adr->mobile;
                                        if ($product->shiping_on == 'DEALER')
                                            $save['shiping'] = 0;
                                        elseif ($product->shiping_on == 'BETWEEN')
                                            $shiping = ($product->shiping_unsuport * $item->amount) / 2;
                                        else
                                            $save['shiping'] = $product->shiping_unsuport * $item->amount;
                                        $save['weight'] = $product->shiping_weghite * $item->amount;
                                        $save['total_price'] = $item->price + $save['shiping'];
                                        $this->invitem->save($save, array('id' => $item->id), TRUE);
                                    }
                                }
                            }
                        }
                    }
                    $items = $this->invitem->get(array('inv_num' => $data->inv_num));
                }
            }
        } else {
            $save['session_id'] = $this->session->userdata('session_id');
            $save['title'] = 'label_buy_product';
            $save['itemes'] = 'YES';
            $save['type'] = 'basket';
            $save['inv_num'] = $this->unique_id();
            $this->basket->save($save);
            redirect(current_url());
        }
        $this->pass['data'] = $data;
        $this->pass['items'] = $items;
        $this->themes->render('cart/index', $this->pass);
    }

    function add($id = false) {
        if ($id) {
            $this->load->model('products_model', 'product');
            $product = $this->product->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
            if ($this->user_id == $product->owner) {
                $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_product_owner'));
            }
            if (check_product_incart($id)) {
                $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_product_in_cart'));
            } else {
                $cartid = check_cart_created();
                if (!$cartid) {
                    $save['session_id'] = $this->session->userdata('session_id');
                    $save['title'] = 'label_buy_product';
                    $save['itemes'] = 'YES';
                    $save['type'] = 'basket';
                    $save['inv_num'] = $this->unique_id();
                    $this->basket->save($save);
                    $cart = $this->basket->get(array('session_id' => $this->session->userdata('session_id')), false, 'row');
                    if ($cart) {
                        $cartid = $cart->inv_num;
                        $save = NULL;
                    } else {
                        $this->themes->set_message(array('type' => 'error', 'url' => $this->agent->referrer(), 'message' => 'message_save_error'));
                    }
                }
                $save['inv_num'] = $cartid;
                $save['product_id'] = $id;
                $save['title'] = $product->name;
                if ($product->discount_precentage > 0)
                    $price = $product->discount_price;
                else
                    $price = $product->price;
                $save['price'] = $price;
                $save['amount'] = '1';
                $save['total_price'] = $price;
                $save['weight'] = $product->shiping_weghite;
                $save['inv_num'] = $cartid;
                if ($this->invitem->save($save)) {
                    $this->themes->set_message(array('type' => 'success', 'url' => 'cart', 'message' => 'message_save_success'));
                } else {
                    get_save_error();
                }
            }
        } else {
            show_404();
        }
    }

    private function unique_id() {
        $string = strrev(md5(time() + $this->session->userdata('session_id')));
        $unique = substr($string, 0, 9);
        $temp = $this->basket->get(array('inv_num' => $unique));
        if (count($temp) > 0) {
            $this->unique_id();
        } else {
            return $unique;
        }
        return false;
    }

    function remove($id = false) {
        if ($id) {
            $this->load->model('products_model', 'product');
            $product = $this->product->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
            if ($cartid = check_product_incart($id)) {
                $this->invitem->delete(array('product_id' => $id, 'inv_num' => $cartid), FALSE);
            }
            $this->themes->set_message(array('type' => 'success', 'url' => $this->agent->referrer(), 'message' => 'message_save_success'));
        } else {
            show_404();
        }
    }

    function update() {
        if ($this->input->post()) {
            $data = $this->basket->get(array('session_id' => $this->session->userdata('session_id')), false, 'row');
            if (count($data) > 0) {
                if (!$this->input->post('address')) {
                    redirect(__CLASS__);
                    return;
                }
                $addresses = $this->input->post('address');
                $amount = $this->input->post('amount');
                $color = $this->input->post('color');
                $size = $this->input->post('size');
                $shipingtype = $this->input->post('shipingtype');
                $cities = get_shiping_cities_id();
                $items = $this->invitem->get(array('inv_num' => $data->inv_num));
                foreach ($items as $item) {
                    $save['amount'] = $amount[$item->id];
                    $save['address'] = $addresses[$item->id];
                    $adr = get_one_addressbook($save['address']);
                    $save['city_id'] = $adr->cities_id;
                    $save['mobile'] = $adr->mobile;
                    $product = get_product_from_id($item->product_id);
                    if ($product->discount_precentage > 0)
                        $save['total_price'] = $price = $product->discount_price * $amount[$item->id];
                    else
                        $save['total_price'] = $price = $product->price * $amount[$item->id];
                    if (in_array($adr->cities_id, $cities)) {
                        $weghite = $product->shiping_weghite * $amount[$item->id];
                        $max = get_shiping_max_weight();
                        $price = get_shiping_price();
                        if ($weghite <= $max) {
                            $shiping = $price;
                        } else {
                            $weghitover = $weghite - $max;
                            $shiping = $price + ($weghitover * get_shiping_price_for_unit());
                        }
                        if ($product->shiping_on == 'DEALER')
                            $shiping = 0;
                        elseif ($product->shiping_on == 'BETWEEN')
                            $shiping = $shiping / 2;
                        $save['shiping'] = $shiping;
                        $save['weight'] = $weghite;
                        $save['total_price'] += $shiping;
                        if ($shipingtype[$item->id] == 'gethand') {
                            $save['get_by_hand'] = 'YES';
                            $save['cash_on_delivery'] = 'NO';
                            $save['shiping'] = 0;
                            $save['total_price'] = $price;
                        } elseif ($shipingtype[$item->id] == 'cashdelivery') {
                            $save['get_by_hand'] = 'NO';
                            $save['cash_on_delivery'] = 'YES';
                            $save['shiping'] += get_cash_on_delivery_cost();
                            $save['total_price'] += get_cash_on_delivery_cost();
                        } else {
                            $save['get_by_hand'] = 'NO';
                            $save['cash_on_delivery'] = 'NO';
                        }
                    } else {
                        if ($product->shiping_on == 'DEALER')
                            $save['shiping'] = 0;
                        elseif ($product->shiping_on == 'BETWEEN')
                            $shiping = ($product->shiping_unsuport * $amount[$item->id]) / 2;
                        else
                            $save['shiping'] = $product->shiping_unsuport * $amount[$item->id];
                        $save['weight'] = $product->shiping_weghite * $amount[$item->id];
                        $save['total_price'] += $save['shiping'];
                        if ($shipingtype[$item->id] == 'gethand') {
                            $save['get_by_hand'] = 'YES';
                            $save['cash_on_delivery'] = 'NO';
                            $save['shiping'] = 0;
                            $save['total_price'] = $price;
                        } else {
                            $save['get_by_hand'] = 'NO';
                            $save['cash_on_delivery'] = 'NO';
                        }
                    }

                    if (isset($color[$item->id]))
                        $save['color'] = $color[$item->id];
                    if (isset($size[$item->id]))
                        $save['size'] = $size[$item->id];

                    $save['total_price'] -= $item->discount;
                    $this->invitem->save($save, array('id' => $item->id), TRUE);
                }
            } else {
                $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_no_data_found'));
            }
        }
        redirect(__CLASS__);
    }

    function checkout() {
        $data = $this->basket->is_found(array('session_id' => $this->session->userdata('session_id')));
        $items = $this->invitem->get(array('inv_num' => $data->inv_num));
        $this->pass['inv'] = $data;
        $this->pass['items'] = $items;

        if ($this->input->post()) {
            $price = 0;
            $shiping = 0;
            $discount = 0;
            foreach ($items as $item) {
                $price += $item->price * $item->amount;
                $shiping += $item->shiping;
                $discount += $item->discount;
            }
            foreach ($items as $item) {
                if (!$item->address) {
                    $this->themes->set_message(array('type' => 'error', 'url' => __CLASS__, 'message' => 'message_no_address_found'));
                }
            }
            $save['user_id'] = $this->user_id;
            $save['total'] = $price;
            $save['shiping'] = $shiping;
            $save['discount'] = $discount;
            $save['must_pay'] = $price + $shiping - $discount;
            $save['create_date'] = date("Y-m-d H:i:s");
            $save['type'] = 'inv';
            $save['for'] = 'buyproduct';
            $this->load->model('address_model', 'addre');
            $address = $this->addre->get(array('id' => $item->address), false, 'row');
            if ($address) {
                $save['address'] = $address->address;
                $save['city_id'] = $address->cities_id;
                $save['mobile'] = $address->mobile;
            }
            if ($this->basket->save($save, array('id' => $data->id), TRUE)) {
                $this->session->set_userdata('saved', 'any');
                redirect('index/checked');
                return;
            } else {
                get_save_error();
            }
        }

        $this->themes->render('cart/checkout', $this->pass);
    }

}

